package dk.pun.aot.client.presenter;

import com.google.gwt.event.shared.HandlerManager;
import com.google.gwt.user.client.ui.HasWidgets;

import dk.pun.aot.client.event.CharacterSelectedEvent;
import dk.pun.aot.client.event.CharacterSelectedEventHandler;
import dk.pun.aot.client.event.SelectCharacterEvent;
import dk.pun.aot.client.event.SystemMessageUpdatedEvent;
import dk.pun.aot.client.event.ViewCharacterEvent;
import dk.pun.aot.client.view.MainView;
import dk.pun.aot.client.view.MainView.MainViewTab;

public class MainPresenter implements Presenter, MainView.Presenter {

	private HandlerManager eventBus;
	private MainView view;

	public MainPresenter(HandlerManager eventBus, MainView view) {
		this.eventBus = eventBus;
		this.view = view;
		this.view.setPresenter(this);
		this.view.setSelectedCharacter(null);
		this.bind();
	}
	
	private void bind() {
		this.eventBus.addHandler(CharacterSelectedEvent.TYPE, new CharacterSelectedEventHandler() {
			
			@Override
			public void onCharacterSelected(CharacterSelectedEvent event) {
				view.setSelectedCharacter(event.getCharacterId());
			}
		});
	}

	@Override
	public void onViewCharacter() {
		this.eventBus.fireEvent(new ViewCharacterEvent());
	}

	@Override
	public void onSelectCharacter() {
		this.eventBus.fireEvent(new SelectCharacterEvent());
	}

	@Override
	public void onSystemMessageUpdated(String message) {
		this.eventBus.fireEvent(new SystemMessageUpdatedEvent(message));
		this.setMessage(message);
	}
	
	@Override
	public void go(HasWidgets container) {
		container.clear();
		container.add(this.view.asWidget());
	}

	@Override
	public void setTabFocus(MainViewTab tab) {
		this.view.setTabFocus(tab);
	}

	@Override
	public void setMessage(String message) {
		this.view.setMessage(message);
	}

}
